package com.credit.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.credit.service.CreditBaseParamService;

import net.sf.json.JSONObject;

public class ParamsMapUtil {

	private static final Log log = LogFactory.getLog(ParamsMapUtil.class);
	private static final String integer_regex = "^-?\\d+$";
	
	public static Map<String, Object> replace(Map<String, Object> paramsMap) {
		
		for(String param:paramsMap.keySet()){
			
			Object object = paramsMap.get(param);
			
			if(object instanceof Map){
				replace((Map<String, Object>)object);
			}else{
				
				if(CreditBaseParamService.paramsMap.containsKey(param)&&String.valueOf(object).matches(integer_regex)){
					
					String label = getLabel(param,String.valueOf(object));
					
					Map<String,Object> returnMap = new HashMap<>();
					returnMap.put("value", object);
					returnMap.put("label", label);
					paramsMap.put(param, returnMap);
				}
				
			}
			
		}
		return paramsMap;
	}

	public static String getLabel(String param,String paramValue) {
		Map<String,Object> map = (Map<String, Object>) CreditBaseParamService.paramsMap.get(param);
		
		List paramList = (List) map.get("list");
		String label = "";
		for(int i=0;i<paramList.size();i++){
			Map<String,Object> subMap = (Map<String, Object>) paramList.get(i);
			Integer value = (Integer) subMap.get("value");
			
			if(paramValue.equals(String.valueOf(value))){
				label = (String) subMap.get("label");
				return label;
			}
			
		}
		return "";
	}

	public static String getIDNumber(String idNumber) {
		
		return idNumber.substring(0, 12)+"******";
	}

	public static void updateCompanyName(Map<String, Object> paramsMap) {
		
		Map<String, Object> workTypeMap = (Map<String, Object>) paramsMap.get("workType");
		int workType = (int) workTypeMap.get("value");
		
		if(workType == CodeValueUtil.WORKTYPE_COMPANY.intValue()||workType == CodeValueUtil.WORKTYPE_OFFICE){
			
			if(paramsMap.get("extAttribute")==null){
				return;
			}
			
			log.info("-----"+paramsMap.get("extAttribute"));
			
			JSONObject json = JSONObject.fromObject(paramsMap.get("extAttribute"));
			
			//Map<String, Object> extAttributeMap = (Map<String, Object>) paramsMap.get("extAttribute");
			if(json.get("companyName")==null){
				return;
			}
			
			if(json.get("companyName")!=null&&!"null".equals(json.get("companyName").toString())){
				String companyName = (String) json.get("companyName");
				
				if(!StringUtils.isEmpty(companyName)){
					if(companyName.length()>6){
						companyName = companyName.substring(0, 1)+"***"+companyName.substring(5);
					}else if(companyName.length()>=4&&companyName.length()<=6){
						companyName = companyName.substring(0, 1)+"***"+companyName.substring(3);
					}
					json.put("companyName", companyName);
					paramsMap.put("extAttribute", json.toString());
				}
			}

			
		}
		
	}

	public static void main(String[] args){
		String name = "北京神舟航天软件技术有限公司";
		System.out.println(name.substring(0,1));
		System.out.println(name.substring(1,2));
		System.out.println(name.substring(2,3));
		System.out.println(name.substring(3,4));
		System.out.println(name.substring(4,5));
		System.out.println(name.substring(5,6));
		System.out.println(name.substring(0, 1)+"***"+name.substring(5));
	}
	
}
